package org.maxgroundstation.groundstation.view.map;

import java.awt.BorderLayout;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JPanel;
import javax.swing.JSlider;

import org.maxgroundstation.groundstation.GroundStation;

/**
 * A panel for displaying a local GPS map of the rover's travel.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class MapPanel extends JPanel {

    // Data members
    private MapControlPanel mapControlPanel;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public MapPanel(GroundStation gs) {
        // Use JPanel constructor.
        super();
        
        // Set the layout.
        setLayout(new BorderLayout());
        
        // Create the map view panel.
        MapViewPanel mapViewPanel = new MapViewPanel(gs);
        add(mapViewPanel, BorderLayout.CENTER);
        
        // Create the map control panel.
        mapControlPanel = new MapControlPanel(mapViewPanel);
        add(mapControlPanel, BorderLayout.NORTH);
        
        // Set mouse wheel listener to zoom slider.
        addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent evt) {
                int numClicks = evt.getWheelRotation();
                updateZoomSlider(numClicks);
            }
        });
    }
    
    /**
     * Update the zoom slider with the mouse wheel.
     * @param numClicks the mouse wheel clicks (positive or negative).
     */
    private void updateZoomSlider(int numClicks) {
        JSlider zoomSlider = mapControlPanel.getZoomSlider();
        if (numClicks > 0) {
            // Move zoom slider down.
            if (zoomSlider.getValue() > zoomSlider.getMinimum()) 
                zoomSlider.setValue(zoomSlider.getValue() - 1);
        }
        else if (numClicks < 0) {
            // Move zoom slider up.
            if (zoomSlider.getValue() < zoomSlider.getMaximum()) 
                zoomSlider.setValue(zoomSlider.getValue() + 1);
        }
    }
}